package com.lc.demo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.demo.bean.Cart;
import com.lc.demo.bean.Goods;
import com.lc.demo.bean.Order;
import com.lc.demo.bean.User;
import com.lc.demo.result.ResponseMessage;
import com.lc.demo.service.GoodsService;
import com.lc.demo.service.OrderService;
import com.lc.demo.service.UserService;
import com.lc.demo.util.BeanUtil;
import com.lc.demo.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * @Author: sakila
 * @Date: 2022/03/07/20:14
 * @Description:
 */

@RestController
@CrossOrigin
@RequestMapping("/api/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;


    //查询订单列表
    @GetMapping()
    public ResponseMessage getOrderList(Integer page, HttpServletRequest request) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind = new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);

        PageHelper.startPage(page, 10);
        List<Order> orderList = orderService.selectOrderList(one.getId());


        List<Map<String, Object>> returnList = new ArrayList<>();
        for (Order order : orderList) {

            Map<String, Object> stringObjectMap = BeanUtil.transBean2Map(order);
            List<Map<String, Object>> detailList = (List<Map<String, Object>>) JSON.parse(order.getGoodsDetail());
            List<Map<String, Object>> goodsList = (List<Map<String, Object>>) JSON.parse(order.getGoodsDetail());
            Map<String, Object> goodsMap = new HashMap<>();
            for (Map<String, Object> objectMap : detailList) {
                Integer goodsNum = (Integer) objectMap.get("goodsNum");
                Integer goodsId = (Integer) objectMap.get("goodsId");
                Goods goods = goodsService.selectGoodsByGoodsId(goodsId);
                goodsMap.put("goosNum", goodsNum);
                goodsMap.put("goods", goods);
                goodsList.add(goodsMap);
            }
            stringObjectMap.put("goodList", goodsList);
            returnList.add(stringObjectMap);
        }

        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(returnList);
        return ResponseMessage.successWithData(pageInfo);
    }

    @Autowired
    private GoodsService goodsService;

    //查询新增
    @PostMapping()
    public ResponseMessage addOrder(@RequestBody List<Cart> cartList, HttpServletRequest request) throws IntrospectionException, InvocationTargetException, IllegalAccessException {
        String authorization = request.getHeader("Authorization");
        // 5. 请求头中获取令牌
        Claims claims = JwtUtil.parseJWT(authorization);
        String subject = claims.getSubject();
        User userFind = new User();
        userFind.setEmail(subject);
        User one = userService.findOne(userFind);
        Double price = null;
        Order order = new Order();

        List<Map<String, Object>> goodsList = (List<Map<String, Object>>) JSON.parse(order.getGoodsDetail());

        for (Cart cart : cartList) {
            Map<String, Object> goodsMap = new HashMap<>();
            Integer goodsId = cart.getGoodsId();
            Goods goods = goodsService.selectGoodsByGoodsId(goodsId);
            goodsMap.put("goodsId", goods.getId());
            goodsMap.put("goodsNum", cart.getGoodsNum());
            price += goods.getPrice() * cart.getGoodsNum();
            goodsList.add(goodsMap);
        }
        order.setGoodsDetail(JSON.toJSONString(goodsList));
        order.setUserId(one.getId());
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setPrice(price);
        orderService.insertOrder(order);

        return ResponseMessage.success();
    }

    //修改订单状态
    @GetMapping("/{id}")
    public ResponseMessage updateOrderStatus(@PathVariable("id") Integer id) {
        orderService.updateOrderStatus(id);
        return ResponseMessage.success();
    }
}
